package com.etc.controller;

import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.etc.dto.AdminDto;
import com.etc.entity.Admin;
import com.etc.tools.AjaxOperateResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(value = "管理的接口", description = "处理管理员操作的后端接口")
public class AdminController {
	
	Logger logger = LoggerFactory.getLogger(AdminController.class);

	@Autowired
	RedisTemplate redisTemplate;

	@PostMapping("admin")
	@ApiOperation(value = "登录", httpMethod = "POST", notes = "登录", response = AjaxOperateResponse.class)
	public AjaxOperateResponse<Admin> login(@RequestBody AdminDto admindto) {
		// 拿到验证码
		String vcode = admindto.getVcode();

		// 调用业务代码去查询
		// redisTemplate =>得到所有captcha开头的key
		Set<String> set = redisTemplate.keys("captcha*");
		boolean flag = false;

		//
		for (String k : set) {
			String v = redisTemplate.opsForValue().get(k) + "";
			if (v.equals(vcode)) {
				flag = true;
				break;
			}
		}

		logger.info("vcode是否验证通过: "+flag);
//		如果验证码验证失败，直接返回信息给login.html
		// 去调用业务代码完成登录的业务(假的)
		Admin admin =  new Admin();
		AjaxOperateResponse<Admin> aor = new AjaxOperateResponse<Admin>(200, "登录成功");
		aor.setObj(admin);
		return aor;
	}
}
